/**
*****************************************************************************
 * DESC ：. 
 * Author : Junwei Zhang 
 * CREATEDATE ：2012-8-26 
 * MODIFYLIST ：       	Name 			 Date 			Reason/Contents
 * 				------------------------------------------------------ 
 * 				 	Junwei Zhang     2012-8-26		Created.
 ****************************************************************************
**/
package com.me.server;

import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * 
 * <br/> 下午6:05:01 2012-8-26
 * @Author <a href="mailto:mestudying@sina.com">Junwei Zhang</a><br/>
 **/
public class ThreadPool {
	
	private static int DEFAULT_POOL_SIZE = 3;
	private int size = 0;
	/** 线程池. */
	private List<JTalkServerThread> threadPool = null;
	/** 任务队列. */
	private Queue<MessageProcessor> taskQueue = null;

	public ThreadPool(){
		this(DEFAULT_POOL_SIZE);
	}

	public ThreadPool(int poolSize) {
		if(poolSize <= 0){
			this.size  = DEFAULT_POOL_SIZE;
		}else{
			this.size = poolSize;
		}
		this.threadPool  = new ArrayList<JTalkServerThread>();
		this.taskQueue  = new ConcurrentLinkedQueue<MessageProcessor>();
		this.initThreadPool();
	}

	/**
	 * 初始化线程池.
	 */
	private void initThreadPool() {
		synchronized(taskQueue){
			for(int i = 0; i < this.size; i++){
				JTalkServerThread thread = new JTalkServerThread(taskQueue);
				this.threadPool.add(thread);
				thread.start();
			}
		}
	}

	public void addTask(MessageProcessor task) {
		synchronized(taskQueue){
			this.taskQueue.add(task);
			taskQueue.notifyAll();
		}
	}

}
